﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoccerSimulator5000
{
    /// <summary>
    /// class Footballer is the class that holds information about all types of footballers.
    /// The information is, amongst other things the attributes and wage of a footballer.
    /// </summary>
    public class Footballer
    {

        // Standard attributes
        public string name { get; set; }
        public int age { get; set; }
        public int value { get; set; }
        public int wage { get; set; }

        // Attack attributes
        public float shooting { get; set; }
        public float dribbling { get; set; }

        // Midfield attributes
        public float passing { get; set; }
        public float crossing { get; set; }

        
        // Defend attributes
        public float tackling { get; set; }
        public float marking { get; set; }

        // Goalkeeping attributes
        public float reflexes { get; set; }
        public float anticipation { get; set; }


        // Ambigous attributes
        public float heading { get; set; } // Used for attack, midfield and defending footballers.
        public float stamina { get; set; } // Used by all footballers, incl. goalkeepers.
        public float talent { get; set; } // Only used in generation of player (and for training).

        public string preferredPosition { get; set; }



        // Attribute collections
        public float attacking { get; set; }
        public float midfield { get; set; }
        public float defending { get; set; }
        public float goalkeeping { get; set; }

        public string displayMember { get; set; }


        public Footballer()
        {
            this.name = "";
            this.age = 0;
            this.value = 0;
            this.wage = 0;

            this.shooting = 0;
            this.dribbling = 0;

            this.passing = 0;
            this.crossing = 0;

            this.tackling = 0;
            this.marking = 0;

            this.reflexes = 0;
            this.anticipation = 0;

            this.heading = 0;
            this.stamina = 0;
            this.talent = 0;

            CalculateAttacking();
            CalculateMidfield();
            CalculateDefending();
            CalculateGoalkeeping();

            displayMember = string.Format("{0,-25}{1,2}{2,2}", this.name, this.age, this.preferredPosition);
        }

        public Footballer(string position, int age)
        {
            this.talent = Game.random.Next(550, 1000) / 10;
            FootballerName tempName = new FootballerName();
            this.name = tempName.firstName.ToString() + " " + tempName.lastName.ToString();
            int lowerAge = age - 2;
            int upperAge = age + 5;
            this.age = Game.random.Next(lowerAge, upperAge);

            switch (position)
            {
                case "attacker":
                    // Generate random attacker
                    this.shooting = (float)Game.random.Next(550, 1000) / 10;
                    this.dribbling = (float)Game.random.Next(550, 1000) / 10;

                    this.passing = (float)Game.random.Next(0, 750) / 10;
                    this.crossing = (float)Game.random.Next(0, 750) / 10;

                    this.tackling = (float)Game.random.Next(0, 750) / 10;
                    this.marking = (float)Game.random.Next(0, 750) / 10;

                    this.reflexes = (float)Game.random.Next(0, 750) / 10;
                    this.anticipation = (float)Game.random.Next(0, 750) / 10;

                    this.heading = (float)Game.random.Next(0, 1000) / 10;
                    this.stamina = (float)Game.random.Next(0, 1000) / 10;

                    break;
                case "midfielder":
                    // Generate random midfielder
                    this.shooting = (float)Game.random.Next(0, 750) / 10;
                    this.dribbling = (float)Game.random.Next(0, 750) / 10;

                    this.passing = (float)Game.random.Next(550, 1000) / 10;
                    this.crossing = (float)Game.random.Next(550, 1000) / 10;

                    this.tackling = (float)Game.random.Next(0, 750) / 10;
                    this.marking = (float)Game.random.Next(0, 750) / 10;

                    this.reflexes = (float)Game.random.Next(0, 750) / 10;
                    this.anticipation = (float)Game.random.Next(0, 750) / 10;

                    this.heading = (float)Game.random.Next(0, 1000) / 10;
                    this.stamina = (float)Game.random.Next(0, 1000) / 10;
                    break;
                case "defender":
                    // Generate random defender
                    this.shooting = (float)Game.random.Next(0, 750) / 10;
                    this.dribbling = (float)Game.random.Next(0, 750) / 10;

                    this.passing = (float)Game.random.Next(0, 750) / 10;
                    this.crossing = (float)Game.random.Next(0, 750) / 10;

                    this.tackling = (float)Game.random.Next(550, 1000) / 10;
                    this.marking = (float)Game.random.Next(550, 1000) / 10;

                    this.reflexes = (float)Game.random.Next(0, 750) / 10;
                    this.anticipation = (float)Game.random.Next(0, 750) / 10;

                    this.heading = (float)Game.random.Next(0, 1000) / 10;
                    this.stamina = (float)Game.random.Next(0, 1000) / 10;
                    break;
                case "goalkeeper":
                    // Generate random goalkeeper
                    this.shooting = (float)Game.random.Next(0, 750) / 10;
                    this.dribbling = (float)Game.random.Next(0, 750) / 10;

                    this.passing = (float)Game.random.Next(0, 750) / 10;
                    this.crossing = (float)Game.random.Next(0, 750) / 10;

                    this.tackling = (float)Game.random.Next(0, 750) / 10;
                    this.marking = (float)Game.random.Next(0, 750) / 10;

                    this.reflexes = (float)Game.random.Next(550, 1000) / 10;
                    this.anticipation = (float)Game.random.Next(550, 1000) / 10;

                    this.heading = (float)Game.random.Next(0, 1000) / 10;
                    this.stamina = (float)Game.random.Next(0, 1000) / 10;
                    break;
                default:
                    break;
            }

            CalculateAttacking();
            CalculateMidfield();
            CalculateDefending();
            CalculateGoalkeeping();

            CalculateValue();
            CalculateWage();

            CalculatePreferredPosition();

            displayMember = string.Format("{0,-27}{1,2}{2,3}", this.name, this.age, this.preferredPosition);
        }


        private void CalculateAttacking()
        {
            this.attacking = (this.shooting + this.dribbling + this.heading + this.stamina) / 4;
        }

        private void CalculateMidfield()
        {
            this.midfield = (this.passing + this.crossing + this.heading + this.stamina) / 4;
        }

        private void CalculateDefending()
        {
            this.defending = (this.tackling + this.marking + this.heading + this.stamina) / 4;
        }

        private void CalculateGoalkeeping()
        {
            this.goalkeeping = (this.reflexes + this.anticipation + this.stamina) / 3;
        }

        private void CalculateValue()
        {
            // Calculate base score for footballer
            float totalScore = (this.attacking + this.midfield + this.defending + this.goalkeeping) / 4;
            int ageFactor = this.age - 22;
            int ageValueAdd = 0;

            int talentValueAdd = ((( (int)this.talent) / 10) * 10000);

            if (ageFactor <= 0)
            {
                ageValueAdd = Math.Abs(ageFactor) * 50000;
            }
            else
            {
                ageValueAdd = -(ageFactor * 10000);
            }

            this.value = (int)(totalScore * 60000) + talentValueAdd + ageValueAdd;


        }

        private void CalculateWage()
        {
            this.wage = this.value / 750;
        }

        private void CalculatePreferredPosition()
        {
            List<float> temp = new List<float>();
            temp.Add(this.attacking);
            temp.Add(this.midfield);
            temp.Add(this.defending);
            temp.Add(this.goalkeeping);
            temp.Sort();
            temp.Reverse();

            if (temp[0] == attacking)
                this.preferredPosition = "A";
            else if (temp[0] == midfield)
                this.preferredPosition = "M";
            else if (temp[0] == defending)
                this.preferredPosition = "D";
            else
                this.preferredPosition = "GK";
        }


        public void Draw( System.Drawing.Graphics g, int x, int y )
        {

            System.Drawing.Font theFont = new System.Drawing.Font("Courier New", 9);

            g.FillRectangle(System.Drawing.Brushes.Beige, x, y, 280, 100);
            string lineZero = string.Format("{0, -22} {1, 3:D}", this.name, this.age);
            string lineOne = string.Format("{0,-13} {1, 4:F1} {2,-13} {3, 4:F1}", "Shooting:", this.shooting, "Dribbling:", this.dribbling);
            string lineTwo = string.Format("{0,-13} {1, 4:F1} {2,-13} {3, 4:F1}", "Crossing:", this.crossing, "Tackling:", this.tackling);
            string lineThree = string.Format("{0,-13} {1, 4:F1} {2,-13} {3, 4:F1}", "Reflexes:", this.reflexes, "Anticipation:", this.anticipation);
            string lineFour = string.Format("{0,-13} {1, 4:F1} {2,-13} {3, 4:F1}", "Passing:", this.passing, "Stamina:", this.stamina);
            string lineFive = string.Format("{0,-13} {1, 4:F1} {2,-13} {3, 4:F1}", "Marking:", this.marking, "Talent:", this.talent);
            string lineSix = string.Format("{0,-13} {1, 4:F1} {2,-13} {3, 4:F1}", "Heading:", this.heading, "Position:", this.preferredPosition);
            string lineSeven = string.Format("{0,-6} {1, 11:D} {2,-6} {3, 11:D}", "Value:", this.value, "Wage:", this.wage);

            g.DrawString(lineZero, new System.Drawing.Font("Courier New", 11), System.Drawing.Brushes.Black, x + 2, (y + 3));
            g.DrawString(lineSeven, theFont, System.Drawing.Brushes.Black, x + 2, (y + 3 + (1 * 11)));
            g.DrawString(lineOne, theFont, System.Drawing.Brushes.Black, x + 2, (y + 14 + (2 * 9)));
            g.DrawString(lineTwo, theFont, System.Drawing.Brushes.Black, x + 2, (y + 14 + (3 * 9)));
            g.DrawString(lineThree, theFont, System.Drawing.Brushes.Black, x + 2, (y + 14 + (4 * 9)));
            g.DrawString(lineFour, theFont, System.Drawing.Brushes.Black, x + 2, (y + 14 + (5 * 9)));
            g.DrawString(lineFive, theFont, System.Drawing.Brushes.Black, x + 2, (y + 14 + (6 * 9)));
            g.DrawString(lineSix, theFont, System.Drawing.Brushes.Black, x + 2, (y + 14 + (7 * 9)));

        }
    }
}
